package edu.msse.signup.ui;

import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import java.io.IOException;

import edu.msse.signup.service.UsersService;

/**
 * Created by IntelliJ IDEA.
 * User: shenry
 * Date: Apr 20, 2008
 * Time: 5:37:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserActivationServlet  extends HttpServlet {
    WebApplicationContext spring;
    private static final long serialVersionUID = -2560989072138239258L;

    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        spring = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UsersService userService = (UsersService) spring.getBean("userService");
       
       try{
            String email = request.getParameter("email");
            Integer key = Integer.valueOf(request.getParameter("key"));

           userService.activateUser(email,key);
           String message = "Activation successful! Login below.";
           request.setAttribute("message", message);

       }catch(Exception e){
           e.printStackTrace();
           String error ="Invalid user or activation key. " +
                   "Try copying/pasting the url from your activation email.";
           request.setAttribute("error", error);

       }

            RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
			dispatcher.forward(request, response);
    }
}
